package com.qijian.production.controller;

import java.util.List;

import com.qijian.common.utils.DateUtils;
import com.qijian.common.utils.ObjectUtils;
import com.qijian.production.domain.ScWorkOrderDetail;
import com.qijian.production.domain.query.ScWorkOrderDetailQuery;
import com.qijian.production.service.IScWorkOrderDetailService;
import lombok.RequiredArgsConstructor;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qijian.common.annotation.Log;
import com.qijian.common.annotation.PlugAnn;
import com.qijian.common.annotation.PlugParam;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.domain.entity.FlowableApproval;
import com.qijian.common.core.domain.entity.FlowableBack;
import com.qijian.common.enums.ApprovalDescriptionEnum;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.exception.ServiceException;
import com.qijian.common.utils.SecurityUtils;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
/**
 * 生产计划单工序Controller
 *
 * @author zhangcheng
 * @date 2025-03-25
 */
@Api(tags = "生产计划单工序")
@RestController
@RequiredArgsConstructor
@RequestMapping("/production/workOrderDetail")
public class ScWorkOrderDetailController extends BaseController {

    private final IScWorkOrderDetailService scWorkOrderDetailService;

    /**
     * 查询生产计划单工序列表
     */
    @PreAuthorize("@ss.hasPermi('production:workOrderDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScWorkOrderDetailQuery query, Page<ScWorkOrderDetail> page) {
        initOrderItem(ScWorkOrderDetail.class, page.orders());
        return getDataTable(scWorkOrderDetailService.page(page, WhereEntityTool.invoke(query)));
    }

    /**
     * 导出生产计划单工序列表
     */
    @PreAuthorize("@ss.hasPermi('production:workOrderDetail:export')")
    @Log(title = "生产计划单工序" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(ScWorkOrderDetailQuery query, HttpServletResponse response) {
        new ExcelUtil<>(ScWorkOrderDetail. class).exportExcel(response,scWorkOrderDetailService.list( WhereEntityTool.invoke(query)), "生产计划单工序数据");
    }

    /**
     * 获取生产计划单工序详细信息
     */
    @PreAuthorize("@ss.hasAnyPermi('production:workOrderDetail:list,production:workOrderDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(scWorkOrderDetailService.getById(id));
    }

    /**
     * 新增生产计划单工序
     */
    @PreAuthorize("@ss.hasPermi('production:workOrderDetail:add')")
    @Log(title = "生产计划单工序" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScWorkOrderDetail scWorkOrderDetail) {
        return toAjax(scWorkOrderDetailService.save(scWorkOrderDetail));
    }

    /**
     * 修改生产计划单工序
     */
    @PreAuthorize("@ss.hasPermi('production:workOrderDetail:edit')")
    @Log(title = "生产计划单工序" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScWorkOrderDetail scWorkOrderDetail) {
        approval(scWorkOrderDetailService.getById(scWorkOrderDetail.getId()));
        return toAjax(scWorkOrderDetailService.updateById(scWorkOrderDetail));
    }

    /**
     * 删除生产计划单工序
     */
    @PreAuthorize("@ss.hasPermi('production:workOrderDetail:remove')")
    @Log(title = "生产计划单工序" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        ids.forEach(id -> approval(scWorkOrderDetailService.getById(id)));
        return toAjax(scWorkOrderDetailService.removeByIds(ids));
    }

//    /**
//     * 单据提交审核
//     */
//    @Log(title = "生产计划单工序", businessType = BusinessType.APPROVAL)
//    @PlugAnn(groupClass = FlowableApproval.Approval.class, params = {"'scWorkOrderDetail'", "T(com.qijian.common.utils.SecurityUtils).getUserId()", "T(com.qijian.common.utils.SecurityUtils).getDeptId()", "T(com.qijian.common.utils.SecurityUtils).getNickName()"})
//    @GetMapping(value = "/approval/{id}")
//    public AjaxResult approval(@PlugParam @PathVariable("id") Long id) {
//        approval(scWorkOrderDetailService.getById(id));
//        ScWorkOrderDetail scWorkOrderDetailChange = new ScWorkOrderDetail();
//        scWorkOrderDetailChange.setId(id);
//        scWorkOrderDetailChange.setApprovalStatus(ApprovalDescriptionEnum.B);
//        return AjaxResult.success(scWorkOrderDetailService.updateById(scWorkOrderDetailChange));
//    }
//
//    /**
//     * 单据审核通过
//     */
//    @Log(title = "生产计划单工序", businessType = BusinessType.APPROVAL)
//    @PreAuthorize("@ss.hasPermi('production:workOrderDetail:approved')")
//    @GetMapping(value = "/approved/{id}")
//    public AjaxResult approved(@PathVariable("id") Long id) {
//        ScWorkOrderDetail info = scWorkOrderDetailService.getById(id);
//        if (ObjectUtils.isNull(id) || info.getApprovalStatus() == ApprovalDescriptionEnum.C) {
//            throw new ServiceException("E01:单据已审核！");
//        }
//        ScWorkOrderDetail scWorkOrderDetailChange = new ScWorkOrderDetail();
//        scWorkOrderDetailChange.setId(id);
//        scWorkOrderDetailChange.setApprovalStatus(ApprovalDescriptionEnum.C);
//        scWorkOrderDetailChange.setApprovalBy(SecurityUtils.getNickName());
//        scWorkOrderDetailChange.setApprovalTime(DateUtils.getNowDate());
//        return AjaxResult.success(scWorkOrderDetailService.updateById(scWorkOrderDetailChange));
//    }
//
//    /**
//     * 单据反审核
//     */
//    @Log(title = "生产计划单工序", businessType = BusinessType.APPROVAL)
//    @PreAuthorize("@ss.hasPermi('production:workOrderDetail:deApproval')")
//    @PlugAnn(groupClass = FlowableBack.Approval.class, params = {"'scWorkOrderDetail'"})
//    @GetMapping(value = "/deApproval/{id}")
//    public AjaxResult deApproval(@PathVariable("id") Long id) {
//        ScWorkOrderDetail scWorkOrderDetailChange = new ScWorkOrderDetail();
//        scWorkOrderDetailChange.setId(id);
//        scWorkOrderDetailChange.setApprovalStatus(ApprovalDescriptionEnum.D);
//        return AjaxResult.success(scWorkOrderDetailService.updateById(scWorkOrderDetailChange));
//    }
}
